package user.lan.com.inherit;

import android.annotation.TargetApi;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

import butterknife.ButterKnife;
import user.lan.com.usercenter.R;

/**
 * 代码有问题别找我！虽然是我写的，但是他们自己长歪了。
 *
 * @auth Mr.Jobs
 * @date 2016/2/23
 * @time 10:01
 */
public abstract class ParentActivity extends AppCompatActivity {

    protected int statusBarHeight;
    public String TAG ;
    public Context mContext;
    private ProgressDialog loadDialog;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       //requestWindowFeature(Window.FEATURE_NO_TITLE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
       // getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        setContentView(getResourcesId());
        setFullScreen();
        if (isFullScreen()){
            setFullScreen();
        }
        mContext = getApplicationContext();
        ButterKnife.bind(this);
        TAG = getRunningActivityName();
        init(savedInstanceState);
    }

    protected abstract int getResourcesId();
    protected abstract boolean isFullScreen();
    protected abstract void setFullScreen();
    protected abstract void init(Bundle savedInstanceState);
    private String getRunningActivityName(){
        String contextString = this.toString();
        return contextString.substring(contextString.lastIndexOf(".")+1, contextString.indexOf("@"));
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        dismissDialog();
        loadDialog=null;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    public void showLoadDialog(String message){
        if (loadDialog==null){
            loadDialog= ProgressDialog.show(this, "", message);
        } else if (!loadDialog.isShowing()){
            loadDialog.show();
        }

    }
    public ProgressDialog showReturnLoadDialog(String message){
        if (loadDialog==null){
            loadDialog= ProgressDialog.show(this, "", message);
        } else if (!loadDialog.isShowing()){
            loadDialog.show();
        }
        return loadDialog;
    }
    public void showCancleLoadDialog(String message){
        if (loadDialog==null){
            loadDialog= ProgressDialog.show(this, "", message);
        } else if (!loadDialog.isShowing()){
            loadDialog.show();
        }
        loadDialog.setCancelable(true);
    }
    public void showCancleLoadDialog(){
       showCancleLoadDialog("加载中...");
    }
    public void showLoadDialog(){
        showLoadDialog("加载中");
    }
    public void dismissDialog(){
        if (loadDialog!=null&&loadDialog.isShowing()){
            loadDialog.dismiss();
        }
    }


    /**
     * 设置返回键事件
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            finish();
            overridePendingTransition(R.anim.from_left_to_right_in,
                    R.anim.from_left_to_right_out);
        }
        return super.onKeyUp(keyCode, event);
    }

    public int getStatusBarHeight(Context context) {
        if (statusBarHeight != 0)
            return statusBarHeight;

        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
        }
        return statusBarHeight;
    }
    @TargetApi(Build.VERSION_CODES.KITKAT)
    protected Window setImmerseLayout() {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        statusBarHeight = getStatusBarHeight(this.getBaseContext());
        return window;
    }
}